package com.jsjvm.client.util;

/**
 * Reimplementation of java.io.DataInputStream, but just reads data from a byte
 * array. 
 * 
 * @author ctalau
 *
 */
public class DataInputStream {
	private byte[] data;
	private int pos;

	public DataInputStream(byte[] data) {
		this.data = data;
		this.pos = 0;
	}

	// XXX the int is actually signed
	public static int readUInt(byte [] data, int pos){
		int ch1 = data[pos++] & 0xFF;
		int ch2 = data[pos++] & 0xFF;
		int ch3 = data[pos++] & 0xFF;
		int ch4 = data[pos++] & 0xFF;
		return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));	
	}
	
	public int readUInt() {
		int ret = readUInt(data,pos);
		pos += 4;
		return ret;
	}

	public static int readShort(byte [] data, int pos){
		int ch1 = data[pos++] & 0xFF;
		int ch2 = data[pos++] & 0xFF;
		return (short)((ch1 << 8) | (ch2 << 0));
	}

	public static int readUShort(byte [] data, int pos){
		int ch1 = data[pos++] & 0xFF;
		int ch2 = data[pos++] & 0xFF;
		return ((ch1 << 8) + (ch2 << 0));
	}
	
	public int readUShort() {
		int ret = readUShort(data, pos);
		pos += 2;
		return ret;
	}

	public int read() {
		return data[pos++] & 0xFF;
	}

	public void read(byte[] buf) {
		for (int i = 0; i < buf.length; i++)
			buf[i] = data[pos++];
	}

	
	public static long readLong(byte [] data, int pos){
		long high = readUInt(data, pos) & 0xFFFFFFFFL;
		pos += 4;
		long low  = readUInt(data, pos) & 0xFFFFFFFFL;
		return (high << 32) + low;
		
	}
	public long readLong() {
		long ret = readLong(data, pos);
		pos += 8;
		return ret;
	}

	public void skip(int n){
		pos += n;
	}
}
